 subroutine calciabci_n5(r1, r2, t1, t2, fae, wmbej, & 
               icore)
 !use common
 use mod_iop
 use mod_size
 use mod_orbit
 use mod_ioff 
 use mod_symm
 implicit none
 real*4, intent(in) :: r1(*), r2(*), t1(*), t2(*)
 real*4, intent(in) :: fae(*), wmbej(*), icore(*)
 !
 real*8 tt(20), rt(20)
 integer irrepci, irrepab, numci, numabc, iofft2(8)
 integer irrepi, irrepc, numc, numi, nabsym(8), nabsym2(8)
 integer nsize, irrepj, irrepabi, numj, numabi
 integer irrepabc, irrepabj, iofft2sym, i, ioffi2(8)
 integer ioffi22, iofft22, numabj, numabj1, numabc2
 integer ioffw2(8), ioffw22, numabj2
 integer ioffw3(8), ioffw33, iofft3(8), iofft33
 integer i0, i1, i2, i3, i4, i5, i6, i7, i8
 integer i02, i12, i22, i32, i42, i52, i62
 character*8 fnameaa, fnameab, fnameba, fnamebb
 !
 real*4, allocatable :: itmp(:), r1tmp(:)
 real*4, allocatable :: ttest(:) 
 integer, external :: irpdso, isymoffso, idsymsoc
 real*8, external :: nrm2so
 if(prec==8) then 
    fnameaa = 'ABCIAA__'
    fnameab = 'ABCIAB__'
    fnameba = 'ABCIBA__'
    fnamebb = 'ABCIBB__'
 elseif(prec==4) then 
    fnameaa = 'ABCIAAS_'
    fnameab = 'ABCIABS_'
    fnameba = 'ABCIBAS_'
    fnamebb = 'ABCIBBS_'
 endif
 nsize = max(nvvoo, nvvv)
 i0 = 1               !t2tmp
 i02 = i0 + nsize*isd !t2aa
 i1 = i02 + nsize*isd  !r2tmp
 i2 = i1  + nsize*isd  !itmp
 i3 = i2  + nsize*isd   !twmbej1
 i4 = i3  + nsize*isd  !twmbej2 
 i5 = i4  + nsize*isd  !twmbej3 
 i6 = i5  + nsize*isd 
 i7 = i6  + nsize*isd 
 i8 = i7  + nsize*isd 
 allocate(r1tmp(nifvv*isd), itmp(nifvv*isd))
 !---------------------------------------------------------
 call izero(nabsym, 8)
 do irrepab = 1, nirrep 
    nabsym(irrepab) = irpdso(irrepab, vrta, vrta, 0) 
    nabsym2(irrepab) = irpdso(irrepab, vrta, vrta, 1) 
 enddo
 call myicopyso(t2(ioi3(2)), icore(i0), nioi32)
 call isymtrso(t2(ioi3(2)), icore(i1), vrta, vrta, 1, 1, popa, popa, 1, 0, 1)
 call axpyso(nioi32, -1.d0, icore(i1), 1, icore(i0), 1) 
 call icompso(icore(i0), icore(i5), vrta, vrta, 1, 0, popa, popa, 1, 1, 1)
 call sstgenso(icore(i5), icore(i02), nsize, nabsym, zorb, popa, popa, &
               icore, 1, '1324')
 nsize = idsymsoc(1, vrta, vrta, 0, popa, popa, 1) 
 
 iofft3(1) = 1
 ioffw3(1) = 1
 do irrepj = 1, nirrep-1
    irrepabi = dirprd(irrepj, 1)
    numj = popa(irrepj) 
    numabi = irpdso(irrepabi, nabsym, popa, 1) 
    numabc = irpdso(irrepabi, nabsym, vrta, 1)
    iofft3(irrepj+1) = iofft3(irrepj) + numj*numabi*isd
    numabi = irpdso(irrepabi, nabsym2, popa, 1) 
    ioffw3(irrepj+1) = ioffw3(irrepj) + numj*numabi*isd
 enddo
 nsize = irpdso(1, vrta, popa, 1)
!call zeroso(r1tmp, nsize)
!call myicopyso(r1, r1tmp, nsize)
! write(6,*)'norm of wmbejaaaa', nrm2so(nsize, icore(i3), 1)
 !=======================================================================================
 !------IAbCi---------------
 !------calc of R1AA, R2AB, WEmBj and WEMbj
 !=======================================================================================
 nsize = irpdso(1, vrta, popa, 1)
 !
 call sstgenso(t2(ioi3(2)), icore(i0), nsize, nabsym2, zorb, popa, popb, &
               icore, 1, '1324')
 !
 iofft2(1) = 1
 ioffw2(1) = 1   !EBmj
 ioffw3(1) = 1   !BeJm
 do irrepj = 1, nirrep-1
    irrepabi = dirprd(irrepj, 1)
    numj = popb(irrepj) 
    numabi = irpdso(irrepabi, nabsym2, popa, 1) 
    numabc = irpdso(irrepabi, nabsym2, vrta, 1)
    iofft2(irrepj+1) = iofft2(irrepj) + numj*numabi*isd
    numabi = irpdso(irrepabi, nabsym2, popb, 1) 
    ioffw2(irrepj+1) = ioffw2(irrepj) + numj*numabi*isd
 enddo
 !
 do irrepi = 1, nirrep
    irrepabc = dirprd(irrepi, 1)
    irrepabj = dirprd(irrepi, 1)
    numabc = irpdso(irrepi, nabsym2, vrta, 1)
    numabj = irpdso(irrepi, nabsym2, popa, 1)
    numabj1 = irpdso(irrepi, nabsym, popa, 1)
    numabj2 = irpdso(irrepi, nabsym2, popb, 1)
    numi = popb(irrepi)
    do i = 1, numi
       iofft22 = iofft2(irrepi) + (i-1)*numabj*isd
       call getpqruhf(icore(i2), i, irrepi, 1, fnameab)
 !       write(6,*)'norm of iabci', i, nrm2so(numabc, icore, 1)
     ! -0.5*iabci*t2abij=r1cj   
     !  0.5*iabci*t2abji=r1cj  (iefam*t2efim=r1ai)!
       call VmnpqVmnrs_to_Vpqrs(icore(i2), icore(i0-1+iofft22), irrepi, irrepi, &
                               vrta, vrtb, 1, vrta, zorb, 1, popa, zorb, 1, &
                               r1(ifvo(1)), 1, 1.d0, 1.d0) 
    !  Pij*IAbCi*T1CJ=R1AbJi   (IAbEj*t1EI=r2AbIj)
       call VpqmnVmnrs_to_Vpqrs(icore(i2), t1(ifvo(1)), irrepi, 1, &
                               vrta, zorb, 1, vrta, vrtb, 1, popa, zorb, 1, &
                               icore(i1-1+iofft22), irrepi, 1.d0, 0.d0)
    !     IAbCi*T1bj=WAiCj    (IEfBm*t1fj=WEmBj)
       call sstgenso(icore(i2), icore(i6), nsize, vrta, vrtb, vrta, zorb, &
                     icore, irrepi, '1324')
    !     tmp(ACji)
       ioffw22 = ioffw2(irrepi) + (i-1)*numabj2*isd
       call VpqmnVmnrs_to_Vpqrs(icore(i6), t1(ifvo(1)), irrepi, 1, &
                               vrtb, zorb, 1, vrta, vrta, 1, popb, zorb, 1, &
                               icore(i3-1+ioffw22), irrepi, 1.d0, 0.d0)   
    ! BABA term
    !     IAbcI*T1bj=WAIcj    (IEfbM*t1fj=WEMbj)
    !   =-IbAcI*T1bj
       call sstgenso(icore(i2), icore(i6), nsize, vrtb, vrta, vrtb, zorb, &
                    icore, irrepi, '3214')
    !     tmp(cAjI)
       iofft22 = iofft2(irrepi) + (i-1)*numabj*isd
       call VpqmnVmnrs_to_Vpqrs(icore(i6), t1(ifvo(1)), irrepi, 1, &
                               vrtb, zorb, 1, vrtb, vrta, 1, popb, zorb, 1, &
                               icore(i4-1+iofft22), irrepi, 1.d0, 0.d0)
!----------------------------
       iofft33 = iofft3(irrepi) + (i-1)*numabj1*isd
       ioffw33 = ioffw3(irrepi) + (i-1)*numabj2*isd

       call iassymso(icore(i2), icore(i6), irrepi, vrta, vrta, vrta, zorb, 5)
    !    call getpqruhf(icore(i2), i, irrepi, 1, fnameaa)
    !  0.5*iabci*t2abji=r1cj   (iefam*t2efmi=r1ai)
       call VmnpqVmnrs_to_Vpqrs(icore(i6), icore(i02-1+iofft33), irrepi, irrepi,&
                               vrta, vrta, 0, vrta, zorb, 1, popa, zorb, 1, &
                               r1(ifvo(1)), 1, 1.d0, 1.d0)
    !      IABCI*T1BJ=WAICJ    (IEFBM*t1FJ=WEMBJ)
       call iexpso(icore(i6), icore(i2), vrta, vrta, 0, 1, vrta, zorb, 1, 0,irrepi)
       call sstgenso(icore(i2), icore(i6), nsize, vrta, vrta, vrta, zorb, &
                     icore, irrepi, '1324')
    !     tmp(ACJI)
       call VpqmnVmnrs_to_Vpqrs(icore(i6), t1(ifvo(1)), irrepi, 1, &
                               vrta, zorb, 1, vrta, vrta, 1, popa, zorb, 1, &
                               icore(i5-1+ioffw33), irrepi, 1.d0, 0.d0)

!------------------------------
    enddo
 enddo
 !----R2ABAB
 nsize = idsymsoc(1, vrta, vrtb, 1, popa, popb, 1)
 call sstgenso(icore(i1), icore(i6), nsize, nabsym2, popa, zorb, popb, &
              icore, 1, '1324')
 call iassymso(icore(i6), icore(i2), 1, vrta, vrta, popa, popa, 3)
 call axpyso(nsize, 1.d0, icore(i2),1, r2(ioi3(2)), 1)
 nsize = irpdso(1, vrta, popa, 1)
 !----WEmBj--
 call sstgenso(icore(i3), icore(i6), nsize, nabsym2, popb, zorb, popb, &
                    icore, 1, '1324')
 call sstgenso(icore(i6), icore(i3), nsize, vrta, vrta, popb, popb, &
                    icore, 1, '1423')
 !------FAE
 call sstgenso(icore(i3), icore(i6), nsize, vrta, popb, vrta, popb, &
                    icore, 1, '2431')
 call sumpq(1, popb, vrta, vrta, 1, icore(i6), itmp)
 call axpyso(nifvvaa, 1.d0, itmp, 1, fae(ifvv(1)), 1)
! call axpyso(nifvvaa, 1.d0, fae(ifvv(1)), 1, r1tmp, 1)
! write(6,*)'norm of Faeaa', nrm2so(nifvvaa, r1tmp, 1)

 nsize = idsymsoc(1, vrta, vrta, 1, popb, popb, 1)
 call axpyso(nsize, -1.d0, icore(i3), 1, wmbej(ioi4(2)), 1)
! write(6,*)'norm of wembjabab', nrm2so(nsize, icore(i3), 1)

 !----WEMbj--
 call sstgenso(icore(i4), icore(i6), nsize, nabsym2, popb, zorb, popa, &
                    icore, 1, '1324')
 call sstgenso(icore(i6), icore(i4), nsize, vrtb, vrta, popb, popa, &
                    icore, 1, '2413')
 nsize = idsymsoc(1, vrta, vrtb, 1, popa, popb, 1)
 call axpyso(nsize, 1.d0, icore(i4), 1, wmbej(ioi4(5)), 1)
! write(6,*)'norm of wembjaabb', nrm2so(nsize, icore(i4), 1) 


 !------WEMBJ
 call sstgenso(icore(i5), icore(i6), nsize, nabsym2, popa, zorb, popa, & 
                    icore, 1, '1324')
 call sstgenso(icore(i6), icore(i5), nsize, vrta, vrta, popa, popa, & 
                    icore, 1, '1423')
 !------FAE
 call sstgenso(icore(i5), icore(i6), nsize, vrta, popa, vrta, popa, &
                    icore, 1, '2431')
 call sumpq(1, popa, vrta, vrta, 1, icore(i6), itmp)
 call axpyso(nifvvaa, 1.d0, itmp, 1, fae(ifvv(1)), 1)
 
 nsize = idsymsoc(1, vrta, vrta, 1, popa, popa, 1)
 call axpyso(nsize, -1.d0, icore(i5), 1, wmbej(ioi4(1)), 1) 
 return
 end
